/**
 * 创建人:  @author huangpeng    
 * 创建时间:  2023年08月18日 12:14
 * 项目名称:  lemon-blog-cloud
 * 文件名称:  BlBlogOptionsServiceImpl
 * 文件描述:  @Description: 博客相关service实现类
 * 公司名称:  柠檬
 *
 * All rights Reserved, Designed By 柠檬 
 * @Copyright: 2022-2023
 *
 */
package com.lemon.blog.web.service.impl;

import cn.hutool.core.util.StrUtil;
import com.lemon.blog.web.constants.BusinessConstants;
import com.lemon.blog.web.domain.BlBlogOptions;
import com.lemon.blog.web.mapper.BlBlogOptionsMapper;
import com.lemon.blog.web.service.BlBlogOptionsService;
import com.lemon.common.core.constant.Constants;
import com.lemon.common.core.exception.ServiceException;
import com.lemon.common.security.service.BaseServiceImpl;
import com.lemon.common.security.utils.OptEntityUtils;
import com.lemon.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 包名称：com.lemon.blog.web.service.impl
 * 类名称：BlBlogOptionsServiceImpl
 * 类描述：博客相关service实现类
 * 创建人：@author huangpeng
 * 创建时间：2023年08月18日 12:14
 */
@Service
public class BlBlogOptionsServiceImpl extends BaseServiceImpl<BlBlogOptions> implements BlBlogOptionsService {

    @Autowired
    private BlBlogOptionsMapper blBlogOptionsMapper;

    @Override
    public List<BlBlogOptions> selectBlBlogOptionsList(BlBlogOptions blBlogOptions) {
        return blBlogOptionsMapper.selectBlBlogOptionsList(blBlogOptions);
    }

    @Override
    public Map<String, Integer> insertOrUpdate(BlBlogOptions model) {
        if (model.getBlogId() == null) {
            throw new ServiceException("错误操作！");
        }
        Map<String, Integer> resultMap = new HashMap<>();
        BlBlogOptions blogOptions = new BlBlogOptions();
        blogOptions.setUserId(SecurityUtils.getUserId());
        blogOptions.setBlogId(model.getBlogId());
        List<BlBlogOptions> existModelList = blBlogOptionsMapper.selectBlBlogOptionsList(blogOptions);
        switch (model.getBusinessType()) {
            case BusinessConstants.READ:
                if (existModelList.size() > 0) {
                    BlBlogOptions existModel = new BlBlogOptions();
                    existModel.setId(existModelList.get(0).getId());
                    existModel.setDvRead(Constants.NUM_ONE);
                    OptEntityUtils.preUpdate(existModel);
                    blBlogOptionsMapper.updateByPrimaryKeySelective(existModel);
                    resultMap.put("value", existModel.getDvRead());
                } else {
                    blogOptions.setDvRead(Constants.NUM_ONE);
                    blogOptions.setDvCollection(Constants.NUM_ZERO);
                    blogOptions.setDvLike(Constants.NUM_ZERO);
                    blogOptions.setDvDislike(Constants.NUM_ZERO);
                    OptEntityUtils.preInsert(blogOptions);
                    blBlogOptionsMapper.insertSelective(blogOptions);
                    resultMap.put("value", blogOptions.getDvRead());
                }
                break;
            case BusinessConstants.LIKE:
                if (existModelList.size() > 0) {
                    BlBlogOptions existModel = existModelList.get(0);
                    existModel.setDvLike(Objects.equals(existModel.getDvLike(), Constants.NUM_ZERO) ? Constants.NUM_ONE : Constants.NUM_ZERO);
                    if (existModel.getDvLike() == 1 && existModel.getDvDislike() == 1) {
                        throw new ServiceException("您已经点过踩了");
                    }
                    blBlogOptionsMapper.deleteByBlogIdAndUserId(SecurityUtils.getUserId(), model.getBlogId());
                    OptEntityUtils.preUpdate(existModel);
                    blBlogOptionsMapper.insertSelective(existModel);
                    resultMap.put("value", existModel.getDvLike());
                } else {
                    blogOptions.setDvLike(Constants.NUM_ONE);
                    blogOptions.setDvDislike(Constants.NUM_ZERO);
                    blogOptions.setDvCollection(Constants.NUM_ZERO);
                    OptEntityUtils.preInsert(blogOptions);
                    blBlogOptionsMapper.insertSelective(blogOptions);
                    resultMap.put("value", blogOptions.getDvLike());
                }
                break;
            case BusinessConstants.DISLIKE:
                if (existModelList.size() > 0) {
                    BlBlogOptions existModel = existModelList.get(0);
                    existModel.setDvDislike(Objects.equals(existModel.getDvDislike(), Constants.NUM_ZERO) ? Constants.NUM_ONE : Constants.NUM_ZERO);
                    if (existModel.getDvDislike() == 1 && existModel.getDvLike() == 1) {
                        throw new ServiceException("您已经点过赞了");
                    }
                    blBlogOptionsMapper.deleteByBlogIdAndUserId(SecurityUtils.getUserId(), model.getBlogId());
                    OptEntityUtils.preUpdate(existModel);
                    blBlogOptionsMapper.insertSelective(existModel);
                    resultMap.put("value", existModel.getDvDislike());
                } else {
                    blogOptions.setDvDislike(Constants.NUM_ONE);
                    blogOptions.setDvLike(Constants.NUM_ZERO);
                    blogOptions.setDvCollection(Constants.NUM_ZERO);
                    OptEntityUtils.preInsert(blogOptions);
                    blBlogOptionsMapper.insertSelective(blogOptions);
                    resultMap.put("value", blogOptions.getDvDislike());
                }
                break;
            case BusinessConstants.COLLECTION:
                if (existModelList.size() > 0) {
                    BlBlogOptions existModel = existModelList.get(0);
                    existModel.setDvCollection(Objects.equals(existModel.getDvCollection(), Constants.NUM_ZERO) ? Constants.NUM_ONE : Constants.NUM_ZERO);
                    blBlogOptionsMapper.deleteByBlogIdAndUserId(SecurityUtils.getUserId(), model.getBlogId());
                    OptEntityUtils.preUpdate(existModel);
                    blBlogOptionsMapper.insertSelective(existModel);
                    resultMap.put("value", existModel.getDvCollection());
                } else {
                    blogOptions.setDvCollection(Constants.NUM_ONE);
                    blogOptions.setDvLike(Constants.NUM_ZERO);
                    blogOptions.setDvDislike(Constants.NUM_ZERO);
                    OptEntityUtils.preInsert(blogOptions);
                    blBlogOptionsMapper.insertSelective(blogOptions);
                    resultMap.put("value", blogOptions.getDvCollection());
                }
                break;
            default:
                throw new ServiceException("系统业务参数错误！");
        }
        return resultMap;
    }
}
